package com.example.demo.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(Environment environment) {

//        获取开发环境

        Profiles dev = Profiles.of("dev");

        boolean flag = environment.acceptsProfiles(dev);

        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .enable(flag)//true表示开启swagger
                .select()
                // 扫描所有有注解的api，用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .groupName("will ospreay");//分组

    }

    @Bean
    public Docket createRestApiII(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("okada");
    }

    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //接口文档的标题
                .title("Springboot 整合 Swagger")
                .description("Springboot 整合 Swagger Test")
                .version("1.0")
                // 开发文档的作者信息
                .contact(new Contact("will ospreay", "url", "email"))
                //license 规范
                .license("license")
                .licenseUrl("licenseUrl")
                .build();
    }
}

